<template>
  <a-spin :spinning="pageLoading">
    <a-form-model class="detail-form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item :label="v.title" v-for="(v,i) in form" :key="i">

        <div v-if="v.dataType == 1">
          <a-input allowClear v-model="v.content"/>
        </div>

        <div v-if="v.dataType == 3">
          <a-input-number allowClear v-model="v.content"/>
        </div>

        <div v-if="v.dataType == 2">
          <a-upload
            name="file"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :action="Util.getUploadImagePath()"
            :before-upload="beforeUpload"
            @change="value => handleChange(value, i)"
        >
          <img v-if="v.content" :src="v.content" alt="avatar" style="width: 100%"/>
          <div v-else>
            <div class="ant-upload-text">
              Upload
            </div>
          </div>
        </a-upload>
        </div>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
        <a-button type="primary" @click="onSubmit"> 提交</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-spin>
</template>
<script>
  function getBase64(img, callback) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
  }

  export default {
    data() {
      return {
        pageLoading: false,
        uploadLoading: false,
        labelCol: {span: 5},
        wrapperCol: {span: 14},
        form: [],
      };
    },
    created() {
      this.getData()
    },
    methods: {

      // 获取数据
      getData() {
        this.pageLoading = true
        this.Http.get("/admin/setting/detail").then(res => {
          this.pageLoading = false
          if (res.code === 1) {
            this.form = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },

      // 提交更新
      onSubmit() {
        let data = {}
        this.form.forEach(i => {
          data[i.title_en] = i.content
        })
        this.Http.post("/admin/setting/update", data).then(res => {
          if (res.code == 1) {
            this.getData()
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        })
      },

      // 上传
      handleChange(info, event) {
        console.log(info);
        if (info.file.status === 'uploading') {
          this.uploadLoading = true;
          return;
        }
        if (info.file.status === 'done') {
          // Get this url from response in real world.
          this.$set(this.form[event],"content", info.fileList[0].response.data.path)
          this.uploadLoading = false;
        }
      },

      // 上传前检测格式和大小
      beforeUpload(file) {
        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
        if (!isJpgOrPng) {
          this.$message.error('You can only upload JPG file!');
        }
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('Image must smaller than 2MB!');
        }
        return isJpgOrPng && isLt2M;
      },
    },
  };
</script>
